import { defineConfig,loadEnv } from 'vite'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'


function pathResolve(dir) {
  return resolve(process.cwd(), '.', dir)
};
// https://vitejs.dev/config/
export default defineConfig(({mode,command}) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_ENV,VITE_HTTP_PROXY } = env;
  return{
    // base: VITE_APP_ENV === 'production' ? '/rwx': '/rwx',
    plugins: [
      vue(),
      // 按需引入的配置
      AutoImport({
        resolvers: [ElementPlusResolver()],
        imports:[
              'vue',
              'vue-router',
              'pinia'
        ],
        dts: 'src/auto-import.d.ts', //生成全局文件
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    resolve:{
      alias:{
        '@':pathResolve('src')
      }
    },
    server:{
      host:true,//ip地址
      port:8923, //端口号
      open:false, //启动后是否自动打开浏览器
      proxy: {
        // https://cn.vitejs.dev/config/#server-proxy
        "/zhpApi": {
          target: VITE_HTTP_PROXY,//连接后端本地地址
          changeOrigin: true,
          // rewrite: (p) => p.replace(/^\/dev-api/, ""),
        },
      },
    }
  }
})
